<!DOCTYPE HTML>
<html>

	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="HandheldFriendly" content="true" />
		<meta name="MobileOptimized" content="320" />
		<title>设备列表</title>
        <link rel="stylesheet" href="./css/common.css" type="text/css" charset="utf-8" />
		
		<script type="text/javascript" src="./js/jquery.js"></script>
		<script src="./mqtt/websocket/js/mqttws31.js"></script>
		<script type="text/javascript" src="./js/common.js"></script>
		
	</head>

	<body>
		<header id="header" >
			<div class="iback" onclick="history.go(-1);"></div>
			<div></div>
		</header>
		
		<div class="content">
			<div class="block" style="width: 50%;margin-left: 25%;">
				<div id="status" class="greenColor">未连接</div>
			</div>
			
			<div class="block btn" id="openDiv" onclick="openSheBei()">
				<div>开启</div>
			</div>
			
			<div class="block btn" id="closeDiv" onclick="closeSheBei()">
				<div>关闭</div>
			</div>
			
			
		</div>
		<div class="footer" >
			<div class="block">
				<div>分享</div>
			</div>
			<div class="block" onclick="window.location.href='timing.html'">
				<div>定时</div>
			</div>
		</div>
		<script type="application/javascript">
			
			var sheBeiId=getUrlParam("id");
			var deviceId=sheBeiId;
			var ids=[];
			ids.push(sheBeiId);
			var urlConfig=urlConfig;//在common.js中定义
			
			/**
			 * 打开设备 
			 */
			//{id:"123",command:0,type:"control"}
			function openSheBei(){
                var msgJson={};
                msgJson.id=sheBeiId;
                msgJson.command=1;
                msgJson.type="control";
				sendMqtt(JSON.stringify(msgJson,sheBeiId),sheBeiId);
			}
			
			function closeSheBei(){
				var msgJson={};
                msgJson.id=sheBeiId;
                msgJson.command=0;
                msgJson.type="control";
				sendMqtt(JSON.stringify(msgJson,sheBeiId),sheBeiId);
			}
			
			function queryStatus(){
                    
                connectMqtt(sheBeiId);

			}
			function init(){
			    queryStatus();
			    
			    var sql="select * from device where id='"+deviceId+"'";
				listSql(sql,function(data){
					if(data.length>0){
						$($("#header>div")[1]).html(data[0].name);
						$($(".footer>.block")[1]).attr("onclick","window.location.href='timing.html?id="+data[0].id+"'");
					}
				});
            }
            
            var client;
            function connectMqtt(deviceId){
		      var host = "148.70.214.205";    
		      var port = "61614";
		      var clientId = uuid16(); 
		      var user = "admin";
		      var password = "admin";
		      
		      var destination = deviceId;
		
		    
		      client = new Messaging.Client(host, Number(port), clientId);
		
		      //client.onConnect = onConnect;
		  
		      client.onMessageArrived =function(message){
                  //console.log(message);
                  console.log(message.payloadString);
                  var msg=message.payloadString;
                  msg=JSON.parse(msg);
                  if(msg.id){
                    if(msg.status==1){
                        $("#status").html("正常");
                        $("#status").attr("class","greenColor");
                        // if(data==null){
                        //     $("#status").html("未连接");
                        //     $("#status").attr("class","grayColor");
                        // }else{
                        //     if(data.result==0){
                        //         $("#status").html("未连接");
                        //         $("#status").attr("class","grayColor");
                        //     }
                        //     if(data.status==0){
                        //         $("#status").html("损坏");
                        //         $("#status").attr("class","redColor");
                        //     }
                        //     if(data.status==1){
                        //         $("#status").html("正常");
                        //         $("#status").attr("class","greenColor");
                        //     }
                        //     if(data.status==2){
                        //         $("#status").html("未连接");
                        //         $("#status").attr("class","grayColor");
                        //     }	
                        // }
                    }
                  }
		      };
		      client.onConnectionLost = function(responseObject){
                console.log("ConnectionLost");
                console.log(responseObject);
		      };
		
		      client.connect({
		        userName:user, 
		        password:password, 
		        onSuccess:function(){
                    console.log("connectSuccess");
                    client.subscribe(destination);
		        }, 
		        onFailure:function(p){
                    console.log(p);
		        	console.log("connectFailure");
		        }
		      }); 
            }
            
             function sendMqtt(text,deviceId) {
                if (text) {
                    var message = new Messaging.Message(text);
                    message.destinationName = deviceId;
                    client.send(message);
                }
            }


			init();
		</script>
	</body>
</html>